﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Repository;
using Microsoft.Extensions.DependencyInjection;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Service
{
    [UseDI(ServiceLifetime.Scoped)]
    public class AdminUserService
    {
        private readonly AdminUserRepository adminUserDal;
        private readonly ICacheManager _cacheManager;
        private readonly string cacheKey;
        public AdminUserService(AdminUserRepository adminUserRepository, ICacheManager cacheManager)
        {
            adminUserDal = adminUserRepository;
            _cacheManager = cacheManager;
            cacheKey = _cacheManager.CacheName() + "UserList_";
        }

        public async Task<AdminUser> CheckUorP(string username, string md5Password)
        {
            return await adminUserDal.CheckUorP(username, md5Password);
        }

        public async Task<int> UpdateLoginInfoAsync(EditAdminUser admin_User, int primaryKeyValue)
        {
            return await adminUserDal.UpdateLoginInfoAsync(admin_User, primaryKeyValue);
        }


        public async Task<Page<ViewAdminUser>> GetPageListAsync(int page, int limit)
        {
            return await adminUserDal.GetPageListAsync(page, limit);
        }

        /// <summary>
        /// 调用ID 及 name 无分页列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<UserIdAndName>> GetUserIdAndNameListAsync()
        {
            List<UserIdAndName> result = _cacheManager.Get<List<UserIdAndName>>(cacheKey + "UserIdAndName");

            if (result == null)
            {
                result = await adminUserDal.GetUserIdAndNameListAsync();
                _cacheManager.Set(cacheKey, result, TimeSpan.FromDays(10));
            }
            return result;
        }

        public async Task<AdminUser> GetAsync(int id)
        {
            return await adminUserDal.GetAsync(id);
        }

        public async Task<bool> GetByUsernameAsync(string username)
        {
            return await adminUserDal.GetByUsernameAsync(username);
        }

        public virtual async Task<object> InsertAsync(AdminUser user)
        {
            _cacheManager.RemoveCacheAsync(cacheKey + "UserIdAndName");
            return await adminUserDal.InsertAsync(user);
        }

        public async Task<EditAdminUser> GetEditAsync(int? id)
        {
            var get = await adminUserDal.GetAsync(id);
            if (get != null)
            {
                EditAdminUser user = new()
                {
                    Id = get.Id,
                    Gid = get.Gid,
                    Username = get.Username,
                    Nickname = get.Nickname,
                    Status = get.Status
                };
                return user;
            }
            return null;
        }


        public async Task<int> UpdateAsync(EditAdminUser admin_User, int id)
        {
            _cacheManager.RemoveCacheAsync(cacheKey + "UserIdAndName");
            return await adminUserDal.UpdateAsync(admin_User, id);
        }
        public async Task<int> PassWordUpdateAsync(EditPassWord editPass, int id)
        {
            return await adminUserDal.PassWordUpdateAsync(editPass, id);
        }


        public async Task<int> DeleteByPriKeyAsync(int id)
        {
            _cacheManager.RemoveCacheAsync(cacheKey + "UserIdAndName");
            return await adminUserDal.DeleteByPriKeyAsync(id);
        }


    }
}
